#include "gprs.h"



/* 以下是接口实现 */
int is_err(char *str)
{
	int i = 0;
	for(i = 0; str[i] != '\0'; i++){
#if 0
		if( str[i] == '>' || str[i] == 'O' && str[i+1] == 'K'){
			printf("&&%s&&", &str[i]);
			return 0;
		}
	}
	printf("*****发送短信失败*****\n'%s'\n**********************\n",str);
	return 1;
#else
	//if( str[i] == 'E' && str[i+2] == 'R' && str[i+4] == 'R' && str[i+5] == ':'){
	if( str[i] == 'E' && str[i+1] == 'R'){
		printf("*****发送短信可能失败*****\n'%s'\n**********************\n",str);
		return 1;
	}
}
return 0;

#endif
}

/* 初始化串口*/
int init_serial(int fd)
{
	struct termios options;
	if(tcgetattr(fd, &options) < 0){
		perror("fail to tcgetattr()....");
		return -1;
	}
	options.c_cflag |= (CLOCAL|CREAD);
	options.c_cflag &= ~CSIZE;
	options.c_cflag &= ~CRTSCTS;
	options.c_cflag |= CS8;
	options.c_cflag &= ~CSTOPB;

	options.c_iflag |= IGNPAR;
	options.c_oflag = 0;
	options.c_lflag = 0;

	if(cfsetispeed(&options, B9600) < 0){
		perror("fail to cfsetispeed()....");
		return -1;
	}
	if(cfsetospeed(&options, B9600) < 0){
		perror("fail to cfsetospeed()....");
		return -1;
	}
	if(tcsetattr(fd,TCSANOW,&options) < 0){
		perror("fail to tcgetattr()....");
		return -1;
	}
	return 0;
}

/* 编码转换 */
int swp(char *str)
{
	int i = 0;
	char temp = '\0';
	size_t num = 0;
	if((strlen(str)%2) == 0){
		num = strlen(str)/2;
	}else{
		strcat(str, "F");	
		num = strlen(str)/2;
	}

	for(i = 0; i < num; i++){
		temp = *str++;
		*(str-1) = *str;
		*str = temp;
		str++;
	}
	return 0;
}

/* 发送SMS */
int send_sms(int fd, pdu_t *pdu, char *content)
{
	char w_buf[128] ={0};
	char r_buf[128] = {0};
	char cmd[48+281] = {0};
	int ret = 0;
	
	/* 1-pdu->_7_content */
	char inbuf[1024] = {0}; /* 需要转换的字符串 */
	size_t insize = sizeof(inbuf);   //输入缓冲区大小
	char outbuf[1024] = {}; /* 存放转换后的字符串 */
	size_t outsize = sizeof(outbuf); //输出缓冲区大小
	strcpy(inbuf,content);
	if(utf8_to_unicode (inbuf,&insize,outbuf,&outsize) < 0 ){
		err_log("fail to utf8_to_unicode()");
	}
	strcpy(pdu->_7_content,outbuf);

	/*2-pdu->_6_len*/
	sprintf(pdu->_6_len, "%02x",strlen(pdu->_7_content)/2);
	/*3-命令 AT*/
	strcpy(w_buf, "AT\r");
	if(write(fd, w_buf, strlen(w_buf)) < 0){
		perror("fail to write");	
		return -1;
	}
	memset(r_buf, '\0', sizeof(r_buf));
	sleep(1);
	if((ret = read(fd, r_buf, sizeof(r_buf))) < 0){
		perror("fail to read");
		return -1;
	}
	printf("%d-read1:%s\n", ret,r_buf);
	if(is_err(r_buf)){return -1;}
	
	/*4-命令 AT+CMEE*/
	strcpy(w_buf, "AT+CMEE=1\r");
	if(write(fd, w_buf, strlen(w_buf)) < 0){
		perror("fail to write");	
		return -1;
	}
	memset(r_buf, '\0', sizeof(r_buf));
	sleep(1);
	if((ret = read(fd, r_buf, sizeof(r_buf))) < 0){
		perror("fail to read");
		return -1;
	}
	printf("%d-read2:%s\n", ret,r_buf);
	if(is_err(r_buf)){return -1;}


	/*5-命令 AT+CMGF*/
	strcpy(w_buf, "AT+CMGF=0\r");
	if(write(fd, w_buf, strlen(w_buf)) < 0){
		perror("fail to write");	
		return -1;
	}
	memset(r_buf, '\0', sizeof(r_buf));
	sleep(1);
	if((ret = read(fd, r_buf, sizeof(r_buf))) < 0){
		perror("fail to read");
		return -1;
	}
	printf("%d-read3:%s\n", ret,r_buf);
	if(is_err(r_buf)){return -1;}

	/*6-命令 AT+CMGS*/
	sprintf(w_buf, "AT+CMGS=%d\r", strlen(pdu->_7_content)/2+15);
	if(write(fd, w_buf, strlen(w_buf)) < 0){
		perror("fail to write");	
		return -1;
	}
	memset(r_buf, '\0', sizeof(r_buf));
	sleep(1);
	if((ret = read(fd, r_buf, sizeof(r_buf))) < 0){
		perror("fail to read");
		return -1;
	}
	printf("%d-read4:%s\n", ret,r_buf);
	if(is_err(r_buf)){return -1;}

	/*7-命令 */
	strcpy(cmd, pdu->_1_part);
	strcat(cmd, pdu->_2_phno_center);
	strcat(cmd, pdu->_3_part);
	strcat(cmd, pdu->_4_phno_recv);
	strcat(cmd, pdu->_5_part);
	strcat(cmd, pdu->_6_len);
	strcat(cmd, pdu->_7_content);
	strcat(cmd, "\x1a");
	if(write(fd, cmd, strlen(cmd)) < 0){
		perror("fail to write");	
		return -1;
	}
	memset(r_buf, '\0', sizeof(r_buf));
	sleep(1);
	if((ret = read(fd, r_buf, sizeof(r_buf))) < 0){
		perror("fail to read");
		return -1;
	}
	printf("%d-read5:%s\n", ret,r_buf);
	if(is_err(r_buf)){return -1;}

	return 0;
}

/* 手机号码设置 */
int set_phno(pdu_t *pdu,int type, char *phno)
{
	char str[13] = {0};
	strcpy(str, phno);

	if(strlen(str) != 11){
		puts("input phone is not 11 bit!");
		return -1;
	}
	if(swp(str) < 0){
		puts("fail tp swp()....");
		return -1;
	}

	if(PHNO_CENT == type){
		strcpy(pdu->_2_phno_center, str);
	}else if(PHNO_RECV == type){
		strcpy(pdu->_4_phno_recv, str);
	}else{
		return -1;
	}

	return 0;
}

int utf8_to_unicode (char *inbuf, size_t *insize, char *outbuf, size_t *outsize)
{

  /* 目的编码, TRANSLIT：遇到无法转换的字符就找相近字符替换
   *           IGNORE ：遇到无法转换字符跳过*/
  char *encTo = "UNICODE//IGNORE";
  /* 源编码 */
  char *encFrom = "UTF-8";

  memset(outbuf,'\0',*outsize);

  iconv_t cd = iconv_open (encTo, encFrom);
  if (cd == (iconv_t)-1){
      perror ("iconv_open");
  }

  if(*insize < strlen(inbuf)){
	  puts("intput data too long");
	  return -1;
  }

  /* 保证inbuf  outbuf不变 */
  char *tmpin = inbuf;
  char *tmpout = outbuf;
  
  /* 记录变值 */
  size_t _insize = strlen(inbuf);
  size_t _outsize = *outsize;

  size_t ret = iconv (cd, &tmpin, &_insize, &tmpout, &_outsize);
  if (ret == -1){
     perror ("iconv");
  }
  
  if(_outsize <= 0){
	  puts("space is not enough");
	  return -1;
  }
  char buf[1024];
  char temp1[10];
  char temp2[10];
  int i = 0;
  int count = *outsize- _outsize;
  
  memset(buf,'\0',sizeof(buf));
  for (i=2; i< count; i++){
	  sprintf(temp1,"%x",outbuf[i]);
	  sprintf(temp2,"%x",outbuf[i+1]);
	  //printf("*******itemp1:%s; temp2:%s\n", temp1, temp2);

	  if(strlen(temp2) == 2){
		  strcat(buf, temp2);
		  //printf("outbuf[%d]=%s\n",i, temp2);
	  }else if(strlen(temp2) ==1){
		  char ch = *temp2;
		  sprintf(temp2, "0%c",ch);
		  strcat(buf, temp2);
		  //printf("outbuf[%d]=%s\n",i, temp2); 
	  }else{
		  strcat(buf, temp2+6);
		  //printf("outbuf[%d]=%s\n",i, temp2); 
	  }

	  if(strlen(temp1) == 2){
		  strcat(buf, temp1);
		  //printf("outbuf[%d]=%s\n",i+1, temp1); 
	  }else if(strlen(temp1) ==1){
		  char ch = *temp1;
		  sprintf(temp1, "0%c",ch);
		  strcat(buf, temp1);
		  //printf("outbuf[%d]=%s\n",i+1, temp1); 
	  }else{
		  strcat(buf, temp1+6);
		  //printf("outbuf[%d]=%s\n",i+1, temp1); 
	  }
	  i++;
  }
  strcpy(outbuf,buf);

  iconv_close (cd);/* 关闭句柄 */

  return 0;
}
